import fs from 'fs';
import path from 'path';
import pino from 'pino';

const logFile = path.join(__dirname, '../logs/app.log');
const logStream = fs.createWriteStream(logFile, { flags: 'a' });

const logger = pino(
    {
        level: 'info',
        timestamp: () => `,"time":"${new Date().toLocaleString()}"`,
    },
    {
        write: (chunk) => {
            const log = JSON.parse(chunk.toString());
            const level = logger.levels.labels[log.level] as string;
            const text = `[${log.time}] ${level.toUpperCase()} ${log.msg}\n`;
            logStream.write(text);
        }
    }
);

export default logger;
